package com.yy.utils;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.yy.entity.TaskId;

import javax.naming.BinaryRefAddr;

/**
 * 合同内容识别
 */
public class ContactRecoOcr {
    /**
     * 下载工具包
     * https://open.iot.10086.cn/ai/code/javaUtil.zip
     *
     */
    /**
     * 生成查询id
     * @param picPath
     * @param token
     * @return
     */
    public static String getTaskId(String picPath,String token) {
        String path = "http://ai.heclouds.com:9090/v1/aiApi/picture/asynOcr/vatInvoiceReco";
        Map<String, Object> params = new HashMap<>();
        //方式一:根据图片http或https地址生成base64
        //String imgURL = "http://xxxx/xxxx/x.jpg";
        //String pic = Base64Img.getImageStrFromUrl(imgURL);
        //方式二:根据本地图片文件生成base64
        String pic = Base64Img.getImageStrFromPath(picPath);
        String[] value = {pic};
        params.put("picture", value);
        try {
            //调用
            String result = HttpUtil.post(path, token, params);
            Gson gson = new Gson();
            TaskId taskId = gson.fromJson(result, TaskId.class);
            return taskId.getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String vatInvoiceRecoOcrResult(String taskId,String token) {
        //服务地址
        String path = "http://ai.heclouds.com:9090/v1/aiApi/picture/asynOcr/vatInvoiceReco/result?taskId="+taskId;
        String result = "";
        try {
            //调用
            Thread.sleep(1000*2);
            result = HttpUtil.get(path, token);
//            System.out.println("识别："+result);
            //当completed 为增值税发票识别完成
//            if(result.contains("\"status\":\"processing\"")){
//                vatInvoiceRecoOcrResult(taskId,token);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

